﻿Shader "Custom/UIImageGradient" {
	Properties{
		[PerRendererData] _MainTex("Sprite Texture",2D) = "white"{}
	_StartColor("Start Color",Color) = (1,1,1,1)
		_EndColor("End Color",Color) = (1,1,1,1)
		[Toggle(VertialDirection)] _VerticalDirection("Vertical Direction",Int) = 0

		[HideInInspector] _StencilComp("Stencil Comparison",Float) = 8
		[HideInInspector] _Stencil("Stencil ID",Float) = 0
		[HideInInspector] _StencilOp("Stencil Operation",Float) = 0
		[HideInInspector] _StencilWriteMask("Stencil Write mask",Float) = 255
		[HideInInspector] _StencilReadMask("Stencil Read Mask",Float) = 255
		[HideInInspector] _ColorMask("Color Mask",Float) = 15

		[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip",Float) = 0
	}
		SubShader{

			Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
			"PreviewType" = "Plane"
			"CanUseSpriteAtlas" = "True"
		}
			Stencil
		{
			Ref[_Stencil]
			Comp[_StencilComp]
			Pass[_StencilOp]
			ReadMask[_StencilReadMask]
			WriteMask[_StencilWriteMask]

		}
			Cull off
			Lighting off
			ZWrite off
			ZTest[unity_GUIZTestMode]
			Blend SrcAlpha OneMinusSrcAlpha
			ColorMask[_ColorMask]

			Pass
		{
			Name "UIImage_Gradient"
			CGPROGRAM
	#pragma vertex vert
	#pragma fragment frag
	#pragma target 2.0

	#include "UnityCG.cginc"
	#include "UnityUI.cginc"

	#pragma multi_compile __ UNITY_UI_ALPHACLIP

			struct appdata_t
	{
		float4 vertex:POSITION;
		float4 color: COLOR;
		float2 texcoord : TEXCOORD0;
		UNITY_VERTEX_INPUT_INSTANCE_ID
	};

	struct v2f
	{
		float4 vertex :SV_POSITION;
		float color : COLOR;
		float2 texcoord : TEXCOORD0;
		float4 worldPosition : TEXCOORD1;
		UNITY_VERTEX_OUTPUT_STEREO
	};

	uniform sampler2D _MainTex;
	uniform fixed4 _TextureSampleAdd;
	uniform float4 _ClipRect;
	uniform fixed4 _StartColor;
	uniform fixed4 _EndColor;

	uniform int _VerticalDirection;

	v2f vert(appdata_t IN)
	{
		v2f OUT;
		UNITY_SETUP_INSTANCE_ID(IN)
			UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT)
			OUT.worldPosition = IN.vertex;
			OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

			OUT.texcoord = IN.texcoord;

		OUT.color = IN.color;

		return OUT;
	}

	fixed4 frag(v2f IN) : SV_Target
	{
		float4 tex = tex2D(_MainTex,IN.texcoord);

		float validFactor = IN.texcoord.x;
		if (_VerticalDirection != 0)
		{
			validFactor = IN.texcoord.y;
		}

		float4 validBlendColor = lerp(_StartColor, _EndColor, validFactor)*IN.color;

		fixed4 color = tex * validBlendColor;

		color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);

#ifdef UNITY__UI_ALPHACLIP
		CLIP(color.a - 0.01);
		#endif
			return color;
	} 
		ENDCG
		}

	}

}

